package com.triones.api.service.interlace.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.triones.api.entity.HelpCategorizeDO;
import com.triones.api.entity.interlace.dto.InterlaceCurrencyChainDO;
import com.triones.api.entity.interlace.dto.InterlaceCurrencyDO;
import com.triones.api.entity.interlace.vo.InterlaceCurrencyChainVO;
import com.triones.api.entity.interlace.vo.InterlaceCurrencyVO;
import com.triones.api.entity.vo.HelpCategorizeVO;
import com.triones.api.mapper.interlace.InterlaceCurrencyChainMapper;
import com.triones.api.mapper.interlace.InterlaceCurrencyMapper;
import com.triones.api.service.interlace.InterlaceCurrencyChainService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 币种链管理
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class InterlaceCurrencyChainServiceImpl implements InterlaceCurrencyChainService {

    @Value("${spring.profiles.active}")
    private String activeProfile;

    private final InterlaceCurrencyMapper interlaceCurrencyMapper;

    private final InterlaceCurrencyChainMapper interlaceCurrencyChainMapper;


    @Override
    public TableDataInfo<InterlaceCurrencyVO> pageList(InterlaceCurrencyDO dto, PageQuery pageQuery) {
        LambdaQueryWrapper<InterlaceCurrencyDO> lqw = new LambdaQueryWrapper<InterlaceCurrencyDO>();
        lqw.eq(StringUtils.isNotEmpty(dto.getCurrency()),InterlaceCurrencyDO::getCurrency,dto.getCurrency())
            .eq(InterlaceCurrencyDO::getEnv,activeProfile)
            .eq(InterlaceCurrencyDO::getStatus,"0")
            .orderByAsc(InterlaceCurrencyDO::getCreateTime);
        Page<InterlaceCurrencyVO> page = interlaceCurrencyMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    @Override
    public InterlaceCurrencyVO getCurrencyDetail(Long id) {
        return interlaceCurrencyMapper.selectVoById(id);
    }

    @Override
    public boolean checkCurrency(InterlaceCurrencyDO dto) {
        boolean exist = interlaceCurrencyMapper.exists(new LambdaQueryWrapper<InterlaceCurrencyDO>()
            .eq(InterlaceCurrencyDO::getCurrency, dto.getCurrency())
            .eq(InterlaceCurrencyDO::getStatus,"0")
            .eq(InterlaceCurrencyDO::getEnv,activeProfile)
            .ne(ObjectUtil.isNotNull(dto.getId()), InterlaceCurrencyDO::getId, dto.getId()));
        return !exist;
    }

    @Override
    public boolean addCurrency(InterlaceCurrencyDO dto) {
        dto.setEnv(activeProfile);
        dto.setCreateTime(System.currentTimeMillis());
        dto.setUpdateTime(System.currentTimeMillis());
        return interlaceCurrencyMapper.insert(dto) > 0;
    }

    @Override
    public boolean editCurrency(InterlaceCurrencyDO dto) {
        dto.setUpdateTime(System.currentTimeMillis());
        return interlaceCurrencyMapper.updateById(dto) > 0;
    }

    @Override
    public boolean removeCurrency(Long id) {
        InterlaceCurrencyDO interlaceCurrencyDO = interlaceCurrencyMapper.selectById(id);
        if (interlaceCurrencyDO != null) {
            throw new SecurityException("币种信息不存在！");
        }
        Long chainCount = interlaceCurrencyChainMapper.selectCount(new LambdaQueryWrapper<InterlaceCurrencyChainDO>()
            .eq(InterlaceCurrencyChainDO::getCurrency, interlaceCurrencyDO.getCurrency())
            .eq(InterlaceCurrencyChainDO::getStatus, "0")
            .eq(InterlaceCurrencyChainDO::getEnv, activeProfile));
        if (chainCount > 0) {
            throw new SecurityException("该币种下面存在链，不允许删除！");
        }
        interlaceCurrencyDO.setStatus("1");
        interlaceCurrencyDO.setUpdateTime(System.currentTimeMillis());
        return interlaceCurrencyMapper.updateById(interlaceCurrencyDO) > 0;
    }

    @Override
    public List<InterlaceCurrencyVO> currencyOptionList() {
        return interlaceCurrencyMapper.selectVoList(new LambdaQueryWrapper<InterlaceCurrencyDO>()
            .eq(InterlaceCurrencyDO::getEnv,activeProfile)
            .eq(InterlaceCurrencyDO::getStatus,"0"));
    }

    @Override
    public TableDataInfo<InterlaceCurrencyChainVO> chainPageList(InterlaceCurrencyChainDO dto, PageQuery pageQuery) {
        LambdaQueryWrapper<InterlaceCurrencyChainDO> lqw = new LambdaQueryWrapper<InterlaceCurrencyChainDO>();
        lqw.eq(InterlaceCurrencyChainDO::getCurrency,dto.getCurrency())
            .eq(StringUtils.isNotEmpty(dto.getChain()),InterlaceCurrencyChainDO::getChain,dto.getChain())
            .eq(InterlaceCurrencyChainDO::getEnv,activeProfile)
            .eq(InterlaceCurrencyChainDO::getStatus,"0")
            .orderByAsc(InterlaceCurrencyChainDO::getCreateTime);
        Page<InterlaceCurrencyChainVO> page = interlaceCurrencyChainMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    @Override
    public InterlaceCurrencyChainVO getChainDetail(Long id) {
        return interlaceCurrencyChainMapper.selectVoById(id);
    }

    @Override
    public boolean checkChain(InterlaceCurrencyChainDO dto) {
        boolean exist = interlaceCurrencyChainMapper.exists(new LambdaQueryWrapper<InterlaceCurrencyChainDO>()
            .eq(InterlaceCurrencyChainDO::getCurrency, dto.getCurrency())
            .eq(InterlaceCurrencyChainDO::getChain,dto.getChain())
            .eq(InterlaceCurrencyChainDO::getStatus,"0")
            .eq(InterlaceCurrencyChainDO::getEnv,activeProfile)
            .ne(ObjectUtil.isNotNull(dto.getId()), InterlaceCurrencyChainDO::getId, dto.getId()));
        return !exist;
    }

    @Override
    public boolean addChain(InterlaceCurrencyChainDO dto) {
        dto.setEnv(activeProfile);
        dto.setCreateTime(System.currentTimeMillis());
        dto.setUpdateTime(System.currentTimeMillis());
        return interlaceCurrencyChainMapper.insert(dto) > 0;
    }

    @Override
    public boolean editChain(InterlaceCurrencyChainDO dto) {
        dto.setUpdateTime(System.currentTimeMillis());
        return interlaceCurrencyChainMapper.updateById(dto) > 0;
    }

    @Override
    public boolean removeChain(Long id) {
        InterlaceCurrencyChainDO currencyChainDO = interlaceCurrencyChainMapper.selectById(id);
        if (currencyChainDO == null) {
            throw new SecurityException("没有查询到链信息！");
        }
        currencyChainDO.setStatus("1");
        currencyChainDO.setUpdateTime(System.currentTimeMillis());
        return interlaceCurrencyChainMapper.updateById(currencyChainDO) > 0;
    }

    @Override
    public List<InterlaceCurrencyChainVO> chainOptionList() {
        LambdaQueryWrapper<InterlaceCurrencyChainDO> lqw = new LambdaQueryWrapper<InterlaceCurrencyChainDO>();
        lqw.select(InterlaceCurrencyChainDO::getChain)
            .eq(InterlaceCurrencyChainDO::getEnv, activeProfile)
            .eq(InterlaceCurrencyChainDO::getStatus, "0")
            .groupBy(InterlaceCurrencyChainDO::getChain);
        return interlaceCurrencyChainMapper.selectVoList(lqw);
    }
}
